package ch1;

import java.util.Random;

import rect.Rect;
import tankbullets.Bullet;
import tool.ImageTool;

public class EnemyTank2 extends Tank implements Enemy,Runnable{
	private int life;
	int speed = 15;
	int Homex;
	int Homey;
	int Herox;
	int Heroy;
	int EnemyTankdirctionHomeX;
	int EnemyTankdirctionHomeY;
	int EnemyTankdirctionHeroX;
	int EnemyTankdirctionHeroY;
	public EnemyTank2() {
		ETTF = true;
		life = 10;
		this.enemyTank = ImageTool.enemy4Tank;
		this.ember = ImageTool.beeBlast;
		width = enemyTank[1].getWidth();
		height = enemyTank[1].getHeight();
		y = 0;
		x = (int)(Math.random()*(1000 - width));
	}
	
	@Override
	public boolean outOfBounds() {
		return x<0 && x>1135 && y<0 && y>610;
	}

	@Override
	public void step() {}
	public boolean shootBy(Bullet b) {
		if(super.shootBy(b)){
			life--;
		}
		return life==0;
	}
	@Override
	public int getScore() {
		return 200;
	}

	@Override
	public void run() {
		while(ETTF){
			if(TankGame.isRun){
				Random ra = new Random();
				try {
					Homex = Home.HomeXY.get(0);
					Homey = Home.HomeXY.get(1);
				} catch (Exception e) {
					Homex = 0;
					Homey = 0;
				}
				try {
					Herox = Hero.TankXY.get(0);
					Heroy = Hero.TankXY.get(1);
				} catch (Exception e) {
					Herox = 0;
					Heroy = 0;
				}
				int xy;
				int sx = ra.nextInt(Math.abs(EnemyTankdirctionHomeX)+1);
				int sy = ra.nextInt(Math.abs(EnemyTankdirctionHomeY)+1);
				EnemyTankdirctionHomeX = Homex-this.x;
				EnemyTankdirctionHomeY = Homey-this.y;
				EnemyTankdirctionHeroX = Herox-this.x;
				EnemyTankdirctionHeroY = Heroy-this.y;
				if(Math.abs(EnemyTankdirctionHomeX)<=ImageTool.home.getWidth() || Math.abs(EnemyTankdirctionHomeY)<=ImageTool.home.getWidth()){
					bulletdir = true;
				}else{
					if(Math.abs(EnemyTankdirctionHeroX)<=ImageTool.tankHero[Direct].getWidth() || Math.abs(EnemyTankdirctionHeroY)<=ImageTool.tankHero[Direct].getWidth()){
						bulletdir = true;
					}else{
						bulletdir = false;
					}
				}
				if(sx>sy){
					if(EnemyTankdirctionHomeX>=0){
						if(Math.abs(EnemyTankdirctionHomeX)>=ImageTool.home.getWidth()/2){
							xy = 0;
						}else{
							if(EnemyTankdirctionHomeY>=0){
								xy = 2;
							}else{
								xy = 3;
							}
						}
					}else{
						if(Math.abs(EnemyTankdirctionHomeX)>=ImageTool.home.getWidth()/2){
							xy = 1;
						}else{
							if(EnemyTankdirctionHomeY>=0){
								xy = 2;
							}else{
								xy = 3;
							}
						}
					}
				}else if(sx<sy){
					if(EnemyTankdirctionHomeY>=0){
						if(Math.abs(EnemyTankdirctionHomeY)>=ImageTool.home.getHeight()/2){
							xy = 2;
						}else{
							if(EnemyTankdirctionHomeX>=0){
								xy = 0;
							}else{
								xy = 1;
							}
						}
					}else{
						if(Math.abs(EnemyTankdirctionHomeY)>=ImageTool.home.getHeight()/2){
							xy = 3;
						}else{
							if(EnemyTankdirctionHomeX>=0){
								xy = 0;
							}else{
								xy = 1;
							}
						}
					}
				}else{
					xy = 4;
				}
				switch(xy){
				case 0:
					EnemyTankdirction = 3;
					if(if_can_move(new Rect(getX()+speed, getY(), getX()+speed+getWidth(), getY()+getHeight())))
						x += speed;
					break;
				case 1:
					EnemyTankdirction = 2;
					if(if_can_move(new Rect(getX()-speed, getY(), getX()-speed+getWidth(), getY()+getHeight())))
						x -= speed;
					break;
				case 2:
					EnemyTankdirction = 1;
					if(if_can_move(new Rect(getX(), getY()+speed, getX()+getWidth(), getY()+speed+getHeight())))
						y += speed;
					break;
				case 3:
					EnemyTankdirction = 0;
					if(if_can_move(new Rect(getX(), getY()-speed, getX()+getWidth(), getY()-speed+getHeight())))
						y -= speed;
					break;
				default:
					break;
				}
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
	}
}
